#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <ctime>
#include "Room.h"//包含Room类的头文件

using namespace std;

vector<Room> rooms;//存储Room对象的内容

int setup()
{
    ifstream file("castle.txt");//从文件中读取数据
    if (!file.is_open())
    {
        cout << "File not found!" << endl;
        return 1;
    }

    int num, exitCount, princess, monster, east, west, south, north;
    string exit;
    while (file >> num >> exitCount)
    {
        vector<string> exits;
        for (int i = 0; i < exitCount; i++)
        {
            file >> exit;
            exits.push_back(exit);//将出口添加到向量中
        }
        file >> east >> west >> south >> north;
        rooms.push_back(Room(num, exitCount, princess, monster, exits, east, west, south, north));
    }
    file.close();
    srand(time(0));//种子值为当前时间，确保每次运行时随机数不同
    int number1 = rand() % 9 + 1;//生成第一个随机数
    int number2;
    rooms[number1].princess = 1;//将第一个随机数对应的房间设为公主所在房间
    while ((number2 = rand() % 9 + 1) == number1)
    {
    }
    rooms[number2].monster = 1;//将第二个随机数对应的房间设为怪物所在房间
    return 0;
}

int main()
{
    setup();//设置房间
    int pr = 0;//当前房间索引
    string command;
    bool findPrincess = false;//是否找到公主
    while (!(findPrincess == true && pr == 0) && !(rooms[pr].monster == 1))//当没有找到公主并且当前房间没有怪物时循环
    {
        if (pr == 0)//如果当前房间为大厅
        {
            cout << "Welcome to the lobby(Room1). There are 2 exits: east, north, " << endl;
        }
        else
        {
            rooms[pr].display();//显示当前房间信息
        }
        if (rooms[pr].princess == 1)//如果当前房间有公主
        {
            cout << "You have found the princess! " << endl;
            cout << "Princess: Thank you for rescuing me, brave adventurer! I am ready to leave with you." << endl;
            cout << "The way out is through the lobby. Keep exploring to find your way back!" << endl;
            rooms[pr].princess = 0;//将公主所在房间的公主状态设为已找到
            findPrincess = true;
        }
        bool go = false;
        while (!go)
        {
            cout << "Enter your command:";
            getline(cin, command);
            if (command == "go east")
            {
                if (rooms[pr].east != 0)
                {
                    pr = rooms[pr].east - 1;//移动到东边的房间
                    go = true;
                }
                else
                {
                    cout << "You can't go there! Please try again." << endl;
                }
            }
            else if (command == "go west")
            {
                if (rooms[pr].west != 0)
                {
                    pr = rooms[pr].west - 1;//移动到西边的房间
                    go = true;
                }
                else
                {
                    cout << "You can't go there! Please try again." << endl;
                }
            }
            else if (command == "go south")
            {
                if (rooms[pr].south != 0)
                {
                    pr = rooms[pr].south - 1;//移动到南边的房间
                    go = true;
                }
                else
                {
                    cout << "You can't go there! Please try again." << endl;
                }
            }
            else if (command == "go north")
            {
                if (rooms[pr].north != 0)
                {
                    pr = rooms[pr].north - 1;//移动到北边的房间
                    go = true;
                }
                else
                {
                    cout << "You can't go there! Please try again." << endl;
                }
            }
            else
                cout << "Unknown command! Please try again." << endl;
        }
    }
    if (rooms[pr].monster == 1)//如果当前房间有怪物
    { 
        cout << "You have been killed by the monster! The game is over." << endl;
        return 0;
    }
    if (findPrincess == true && pr == 0)//如果找到了公主并且返回了大厅
    {
        cout << "Congratulations! You have successfully rescued the princess and escaped the castle. You win!" << endl;
    }
    return 0;
}